<?php
header("Content-type: text/html; charset=utf-8");
$appid = "wx5d13ad141dbb55f5";  
$secret = "ea9efd4b26b769092bb842a1d06a4f1c";  
$code = $_GET["code"];  
  
//通过code换取网页授权access_token 
$get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';  
$curl = curl_init();  
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, $get_token_url); 
$res = curl_exec($curl);  
curl_close($curl);  
$json_obj = json_decode($res,true); //正确时返回的JSON数据包 

//刷新access_token（如果需要）
//$refresh_token=$json_obj['refresh_token'];
//$refresh_token_url='https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$refresh_token;
//$curl = curl_init();  
//curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($curl, CURLOPT_TIMEOUT, 500);
//curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
//curl_setopt($curl, CURLOPT_URL, $get_token_url); 
//$res = curl_exec($curl);  
//curl_close($curl);
//$json_obj = json_decode($res,true); //正确时返回的JSON数据包
 
//根据openid和access_token查询用户信息  
$access_token = isset($json_obj['access_token'])?$json_obj['access_token']:'';  
$openid = isset($json_obj['openid'])?$json_obj['openid']:'';  
$get_user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';  
$curl = curl_init();  
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, $get_user_info_url); 
$res = curl_exec($curl);  
curl_close($curl); 

//解析json  
$user_obj = json_decode($res,true);  
$_SESSION['user'] = $user_obj;

$url='http://m.giant-king.com/testdoc/vivo-ble/vivo.php?openid='.$user_obj['openid'].'#123';
//$url='http://m.giant-king.com/testdoc/vivo-blessing/SHYU.php?openid='.$user_obj['openid'].'&nickname='.$user_obj['nickname'].'&sex='.$user_obj['sex'].'#123';
echo "<script language = 'javascript' type = 'text/javascript' >";
echo "window.location.href = '$url' ";
echo "</script>";




	
?>